const park = require('./routes/park')
const fs = require('fs-extra')
const path = require('path')

const { createFilenameMd5 } = require('./utils')

module.exports = {
  setup (app) {
    // 从本地缓存中读取
    app.use((req, res, next) => {
      const filepath = path.resolve(__dirname, `./data/${createFilenameMd5(req.url + JSON.stringify(req.body))}.json`)
      if (fs.existsSync(filepath)) {
        fs.readFile(filepath).then(data => {
          res.end(data)
        })
      } else {
        next()
      }
    })
    // 本地模拟数据
    app.use('/saasdemopre', park)
  }
}
